// 专家看施工项目页
//  MZY_EX_workViewController.m
//  FullHouse
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_EX_workViewController.h"
#import "NetworkingTool.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "MZY_contentModel.h"
#import "MZY_pictureCell.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define HORIZONTAL [UIScreen mainScreen].bounds.size.width / 414
#define VERTICAL [UIScreen mainScreen].bounds.size.height / 736

@interface MZY_EX_workViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *contentArr;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)UIView *headView;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *detailLabel;
@property(nonatomic, retain)UIImageView *faceImageView;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *workLabel;
@property(nonatomic, retain)UIButton *picButton;
@property(nonatomic, retain)UIImageView *picImageView;
@property(nonatomic, retain)UIImageView *viewImageView;
@property(nonatomic, retain)UILabel *viewCountLabel;
@property(nonatomic, retain)UIImageView *zanImageView;
@property(nonatomic, retain)UILabel *zanCountLabel;
@property(nonatomic, retain)UIImageView *messageImageView;
@property(nonatomic, retain)UILabel *messageLabel;
@property(nonatomic, retain)MBProgressHUD *hud;

@end

@implementation MZY_EX_workViewController

- (void)dealloc{
    [self.tableView release];
    [self.contentArr release];
    [self.picArr release];
    [self.headView release];
    [self.titleLabel release];
    [self.detailLabel release];
    [self.faceImageView release];
    [self.nameLabel release];
    [self.workLabel release];
    [self.picButton release];
    [self.picImageView release];
    [self.viewImageView release];
    [self.viewCountLabel release];
    [self.zanImageView release];
    [self.zanCountLabel release];
    [self.messageImageView release];
    [self.messageLabel release];
    [self.hud release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createData];
    [self createView];
}

#pragma mark 获取数据
- (void)createData{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page?col_id=%@&tab=photo&page=1",self.idStr];
    [NetworkingTool networkingWithStrURL:urlStr type:GETType bodyStr:nil block:^(id result) {
        self.contentArr = [NSMutableArray array];
        MZY_contentModel *content = [[MZY_contentModel alloc]init];
        [content setValuesForKeysWithDictionary:result[@"data"][@"collection_info"][@"info"]];
        [self.contentArr addObject:content];
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:content.cover]];
        self.picImageView.alpha = 0.8;
        self.titleLabel.text = content.col_name;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        self.detailLabel.text = content.title_sub_3;
        self.detailLabel.textColor = [UIColor whiteColor];
        self.detailLabel.textAlignment = NSTextAlignmentCenter;
        [self.faceImageView sd_setImageWithURL:[NSURL URLWithString:content.author_face]];
        self.faceImageView.layer.cornerRadius = 30;
        self.faceImageView.layer.masksToBounds = YES;
        self.nameLabel.text = content.author_nick;
        self.nameLabel.textColor = [UIColor whiteColor];
        self.nameLabel.font = [UIFont systemFontOfSize:15];
        self.nameLabel.textAlignment = NSTextAlignmentRight;
        self.workLabel.text = @"设计师";
        self.workLabel.textColor = [UIColor whiteColor];
        self.workLabel.font = [UIFont systemFontOfSize:13];
        self.workLabel.alpha = 0.6;
        self.messageLabel.text = content.comment_count;
        self.messageLabel.textColor = [UIColor whiteColor];
        self.messageImageView.image = [UIImage imageNamed:@"iconfont-20151209tubiaolianxizhuanhuan21.png"];
        self.zanCountLabel.text = content.liked_count;
        self.zanCountLabel.textColor = [UIColor whiteColor];
        self.zanImageView.image = [UIImage imageNamed:@"iconfont-zan.png"];
        self.viewCountLabel.text = content.view_count;
        self.viewCountLabel.textColor = [UIColor whiteColor];
        self.viewImageView.image = [UIImage imageNamed:@"iconfont-liulan.png"];
        
        [content release];
    }];
    [NetworkingTool networkingWithStrURL:urlStr type:GETType bodyStr:nil block:^(id result) {
        self.picArr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"collection_photos"][@"photos"]) {
            MZY_contentModel *content = [[MZY_contentModel alloc]init];
            [content setValuesForKeysWithDictionary:temp];
            [self.picArr addObject:content];
            [content release];
        }
        [self.tableView reloadData];
        self.hud.hidden = YES;
    }];
}

#pragma mark 创建界面
- (void)createView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    // 创建头视图
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3+ 30)];
    [self.headView release];
    self.picButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.picButton.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 3+ 30);
    [self.picButton addTarget:self action:@selector(detailAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.picButton];
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3+ 30)];
    [self.picButton addSubview:self.picImageView];
    [self.picImageView release];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70 * VERTICAL, WIDTH, 40 *VERTICAL)];
    [self.picImageView addSubview:self.titleLabel];
    [self.titleLabel release];
    self.detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120 * VERTICAL, WIDTH, 20 * VERTICAL)];
    [self.picImageView addSubview:self.detailLabel];
    [self.detailLabel release];
    self.faceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, HEIGHT / 3 - 40 * VERTICAL, 60 * HORIZONTAL, 60 * HORIZONTAL)];
    [self.picImageView addSubview:self.faceImageView];
    [self.faceImageView release];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 50 * HORIZONTAL, 20 * VERTICAL)];
    [self.picImageView addSubview:self.nameLabel];
    [self.nameLabel release];
    self.workLabel = [[UILabel alloc] initWithFrame:CGRectMake(120 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 40 * HORIZONTAL, 20 * VERTICAL)];
    self.workLabel.backgroundColor = [UIColor blueColor];
    [self.picImageView addSubview:self.workLabel];
    [self.workLabel release];
    self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 30 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 20 * HORIZONTAL, 20 * HORIZONTAL)];
    [self.picImageView addSubview:self.messageLabel];
    [self.messageLabel release];
    self.messageImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 55 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 20 * HORIZONTAL, 20 * HORIZONTAL)];
    [self.picImageView addSubview:self.messageImageView];
    [self.messageImageView release];
    self.zanCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 85 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 20 * HORIZONTAL, 20 * HORIZONTAL)];
    [self.picImageView addSubview:self.zanCountLabel];
    [self.zanCountLabel release];
    self.zanImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 110 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 20 * HORIZONTAL, 20 * HORIZONTAL)];
    [self.picImageView addSubview:self.zanImageView];
    [self.zanImageView release];
    self.viewCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 155 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 50 * HORIZONTAL, 20 * HORIZONTAL)];
    [self.picImageView addSubview:self.viewCountLabel];
    [self.viewCountLabel release];
    self.viewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 175 * HORIZONTAL, HEIGHT / 3 - 20 * VERTICAL, 20 * HORIZONTAL, 20 * HORIZONTAL)];
    [self.picImageView addSubview:self.viewImageView];
    [self.viewImageView release];
    
    // 创建"tableView"
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.rowHeight = 260;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headView;
    [self.tableView registerClass:[MZY_pictureCell class] forCellReuseIdentifier:@"table"];
    [self.tableView release];
    
    // 加载动画
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

#pragma mark "tableView"的第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.picArr.count;
}

#pragma mark "tableView"的第二个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MZY_pictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    MZY_contentModel *content = self.picArr[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:content.pic]];
    [cell.likeButton setImage:[UIImage imageNamed:@"iconfont-xin.png"] forState:UIControlStateNormal];
    [cell.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventValueChanged];
    [cell.addButton setImage:[UIImage imageNamed:@"iconfont-jia.png"] forState:UIControlStateNormal];
    [cell.addButton addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventValueChanged];
    [cell.messageButton setImage:[UIImage imageNamed:@"iconfont-20151209tubiaolianxizhuanhuan11-2.png"] forState:UIControlStateNormal];
    [cell.messageButton addTarget:self action:@selector(messageAction:) forControlEvents:UIControlEventValueChanged];
    [cell.shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang.png"] forState:UIControlStateNormal];
    [cell.shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventValueChanged];
    cell.addCountLabel.text = content.col_count;
    cell.messageCountLabel.text = content.comment_count;
    return cell;
}

#pragma mark 返回"button"的点击方法
- (void)backAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark 喜欢按钮的点击方法
- (void)likeAction:(UIButton *)button{
    
}

#pragma mark 添加按钮的点击方法
- (void)addAction:(UIButton *)button{
    
}

#pragma mark 评论按钮的点击方法
- (void)messageAction:(UIButton *)button{
    
}

#pragma mark 分享按钮的点击方法
- (void)shareAction:(UIButton *)button{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
